package com.sendpo.zjx.sendpo.fragment.helpingorder;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.google.gson.Gson;
import com.jingchen.pulltorefresh.PullToRefreshLayout;
import com.jingchen.pulltorefresh.PullableRecyclerView;
import com.sendpo.zjx.sendpo.R;

import com.sendpo.zjx.sendpo.domain.HelpInfoDomain;
import com.sendpo.zjx.sendpo.net.HttpCode;
import com.sendpo.zjx.sendpo.net.HttpUrl;
import com.sendpo.zjx.sendpo.utils.Tools;
import com.sendpo.zjx.sendpo.utils.Xutils;

import org.xutils.common.Callback;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by Administrator on 2017/8/21.
 */

public class MyselHelpOrder2Fragment extends BaseMyselfHelpOrderSingleFragment {


    View rootView = null;
    @BindView(R.id.recyclerview)
    PullableRecyclerView recyclerview;
    @BindView(R.id.pulltofresh)
    public PullToRefreshLayout pulltofresh;
    private LinearLayoutManager lm;
    public Dialog ready = null;
    private View exitbt,readbybt;
    public Dialog paydialog;
    private RadioGroup radioGroup;

    private RecylcerAdaper adpter;
    public int pagenow=1;
    public int currentlastitem=0;
    public boolean lastloading=false;

    @Override
    public View initView() {
        View view = View.inflate(mActivity, R.layout.fragment_helpingorder_recycler, null);
        ButterKnife.bind(view, mActivity);
        return view;
    }


    public void initData() {
        ready = new Dialog(mActivity);
        ready.setContentView(R.layout.dialog_readypay);
        ready.setCanceledOnTouchOutside(false);
        ready.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        exitbt=ready.findViewById(R.id.exitbt);
        exitbt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ready.dismiss();
            }
        });
        readbybt=ready.findViewById(R.id.readbybt);
        readbybt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ready.dismiss();
                paydialog.show();
            }
        });
        initPayDialog();

        lm = new LinearLayoutManager(mActivity, OrientationHelper.VERTICAL, false);
        recyclerview.setLayoutManager(lm);
        List<HelpInfoDomain.DataBean> dataBeanList=new ArrayList<>();
        recyclerview.setAdapter(adpter=new RecylcerAdaper(dataBeanList,mActivity));
        pulltofresh.setPullDownEnable(true);
        pulltofresh.setPullUpEnable(false);
        pulltofresh.setOnPullListener(new PullToRefreshLayout.OnPullListener() {
            @Override
            public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
                pagenow = 1;
                query(pagenow);
            }

            @Override
            public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
                MyselHelpOrder2Fragment.this.pagenow++;
                append(MyselHelpOrder2Fragment.this.pagenow);
            }
        });
        pulltofresh.autoRefresh();
    }

    public void  initPayDialog(){
        paydialog=new Dialog(mActivity);
        paydialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        paydialog.setContentView(R.layout.dialog_rechargemoney);
        radioGroup=paydialog.findViewById(R.id.rg_pay);

        TextView recharage_title=paydialog.findViewById(R.id.recharage_title);
        recharage_title.setText("结算支付");
               EditText recharge_edit= paydialog.findViewById(R.id.recharge_edit);
        recharge_edit.setVisibility(View.GONE);
        View yuyue_left=paydialog.findViewById(R.id.yuyue_left);
        yuyue_left.setVisibility(View.VISIBLE);
        RadioButton pay_moneypackage=paydialog.findViewById(R.id.pay_moneypackage);
        pay_moneypackage.setVisibility(View.VISIBLE);
        pay_moneypackage.setChecked(true);
        RadioButton pay_wexin=paydialog.findViewById(R.id.pay_wexin);
        pay_wexin.setChecked(false);
        paydialog.findViewById(R.id.moneycount_rl);

        Button paybt=paydialog.findViewById(R.id.paybt);

        paybt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                switch (radioGroup.getCheckedRadioButtonId()){
                    case R.id.pay_moneypackage:
                        Toast.makeText(mActivity.getApplicationContext(),"余额支付"
                                ,Toast.LENGTH_SHORT).show();
                        paydialog.dismiss();
                        break;
                    case R.id.pay_wexin:
                        Toast.makeText(mActivity.getApplicationContext(),"微信支付"
                                ,Toast.LENGTH_SHORT).show();
                        paydialog.dismiss();
                        break;
                    case R.id.pay_paypackage:
                        Toast.makeText(mActivity.getApplicationContext(),"支付宝支付",Toast.LENGTH_SHORT).show();
                        paydialog.dismiss();
                        break;
                }
            }
        });
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view

        if(rootView==null){
            rootView = super.onCreateView(inflater, container, savedInstanceState);
            ButterKnife.bind(this, rootView);
            initData();
        }
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();

    }

    public class RecylcerAdaper extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
        public List<HelpInfoDomain.DataBean> dataBeen;
        public LayoutInflater inflater;

        public RecylcerAdaper(List<HelpInfoDomain.DataBean> dataBeen, Context context) {
            inflater = LayoutInflater.from(context);
            this.dataBeen = dataBeen;
        }

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            RecyclerView.ViewHolder ih = null;
            ih = new IndexHoler(inflater.inflate(R.layout.layout_user_needhelppayinfolistitem, parent, false));
            return ih;
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
            final IndexHoler ih = (IndexHoler) holder;
            final HelpInfoDomain.DataBean dataBean=dataBeen.get(position);
             ih.cir_icon.setImageResource(R.drawable.nim_avatar_default);
            Glide.with(MyselHelpOrder2Fragment.this)
                    .load(""+dataBean.getUserAvatarpath())
                    .asBitmap()
                    .placeholder(R.drawable.nim_avatar_default)
                    .centerCrop()
                    .animate(R.anim.fade_in)
                    .override(300,300)
                    .into(new SimpleTarget<Bitmap>() {
                        @Override
                        public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                            ih.cir_icon.setImageBitmap(resource);
                        }
                    });
             ih.uname.setText(dataBean.getUserName()+"");
             if(dataBean.getUserSex()==0){
                 ih.sex_rl.setBackgroundColor(getResources().getColor(R.color.default_blue));
                 ih.sex_iv.setImageResource(R.mipmap.sex_male_while);
             }else{
                 ih.sex_rl.setBackgroundColor(getResources().getColor(R.color.colorAccent));
                 ih.sex_iv.setImageResource(R.mipmap.sex_female_while);
             }
             ih.sex_tv.setText(""+dataBean.getUserAge());
                ih.rootView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Map<String,Object> map=new HashMap<>();
                        map.put("id",dataBean.getUserId());
                    //    Tools.redirectActivity(mActivity, UserPersonCenterActivity.class,map);
                    }
                });
        }
        public void initRefresh(List<HelpInfoDomain.DataBean> domains) {
            this.dataBeen.removeAll(this.dataBeen);
            this.dataBeen.addAll(domains);
            notifyDataSetChanged();
        }
        public void initAppend(List<HelpInfoDomain.DataBean> domains){
            this.dataBeen.addAll(domains);
            notifyDataSetChanged();
        }

        public void delete(int position){
            this.dataBeen.remove(position);
            notifyDataSetChanged();
        }
        @Override
        public int getItemCount() {
            return dataBeen.size();
        }

        public class IndexHoler extends RecyclerView.ViewHolder {
            public View rootView;
            public CircleImageView cir_icon;
            public TextView uname;
            public Button gofukuan_bt;
            public RelativeLayout sex_rl;
            public ImageView sex_iv;
            public TextView sex_tv;
            public IndexHoler(View itemView) {
                super(itemView);
                rootView = itemView;
                uname = itemView.findViewById(R.id.uname);
                cir_icon = itemView.findViewById(R.id.cir_icon);
                gofukuan_bt=itemView.findViewById(R.id.gofukuan_bt);
                gofukuan_bt.setVisibility(View.GONE);
                sex_rl=itemView.findViewById(R.id.sex_rl);
                sex_iv=itemView.findViewById(R.id.sex_iv);
                sex_tv=itemView.findViewById(R.id.sex_tv);
            }
        }


    }




    public void query(int pagenow){
System.out.println();
        Map<String,String> map=new HashMap<>();
        map.put("pagenow",pagenow+"");
        map.put("helpId",mActivity.helpId+"");
        map.put("orderState","2");
        Xutils.POST(HttpUrl.userhelporderbyhelpid, map, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                pulltofresh.refreshFinish(PullToRefreshLayout.SUCCEED);
                Gson gson=new Gson();
                HelpInfoDomain domain=gson.fromJson(result,HelpInfoDomain.class);
                switch (domain.getState()){
                    case HttpCode.SUCCESS:
                        if(domain.getData()!=null&&domain.getData().size()!=0){
                            if(adpter==null){
                                recyclerview.setAdapter(adpter=new RecylcerAdaper(domain.getData(),mActivity));
                            }else{
                                adpter.initRefresh(domain.getData());
                            }

                        }else{
                            if(adpter!=null){
                                List<HelpInfoDomain.DataBean> dataBeanList=new ArrayList<HelpInfoDomain.DataBean>();
                                adpter.initRefresh(dataBeanList);
                            }
                        }
                        break;

                    case HttpCode.ERROR:
                        if(adpter!=null){
                            List<HelpInfoDomain.DataBean> dataBeanList=new ArrayList<HelpInfoDomain.DataBean>();
                            adpter.initRefresh(dataBeanList);
                        }
                        break;

                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                pulltofresh.refreshFinish(PullToRefreshLayout.FAIL);
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    public void append(int pagenow){
        lastloading=true;
        Map<String,String> map=new HashMap<>();
        map.put("pagenow",pagenow+"");
        map.put("helpId",mActivity.helpId+"");
        map.put("orderState","2");
        Xutils.POST(HttpUrl.userhelporderbyhelpid, map, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                pulltofresh.loadmoreFinish(PullToRefreshLayout.SUCCEED);
                lastloading=false;
                Gson gson=new Gson();
                HelpInfoDomain domain=gson.fromJson(result,HelpInfoDomain.class);
                switch (domain.getState()){
                    case HttpCode.SUCCESS:
                        if(domain.getData()!=null&&domain.getData().size()!=0){
                            if(adpter==null){
                                recyclerview.setAdapter(adpter=new RecylcerAdaper(domain.getData(), mActivity));

                            }else{
                                adpter.initAppend(domain.getData());
                            }

                        }
                        break;
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                lastloading=false;
                pulltofresh.loadmoreFinish(PullToRefreshLayout.FAIL);
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }




}
